package until

import "strings"

//此模块用于数据进行空气站单位转换匹配

const (
	so2 = 64.063
	no2 = 46.01
	co  = 28.01
	no  = 30.006
	o3  = 47.9982
	Twc = 0.916
)

func PPBtoPPM(input float64) float64 {
	return input * 1000
}

func PPMtoPPB(input float64) float64 {
	return input / 1000
}

func Ugm3toPPb(input float64, ingredient string) float64 {
	switch strings.ToUpper(ingredient) {
	case "SO2":
		return input * 22.4 / so2
	case "O3":
		return input * 22.4 / o3
	case "NO":
		return input * 22.4 / no
	case "NO2":
		return input * 22.4 / no2
	case "CO":
		return input * 22.4 / co
	default:
		return 0
	}
}

func Ugm3toPPM(input float64, ingredient string) float64 {
	return PPBtoPPM(Ugm3toPPb(input, ingredient))
}

func Ugm3toMgm3(input float64) float64 {
	return input * 1000
}

func Mgm3toUgm3(input float64) float64 {
	return input / 1000
}

func Mgm3toPPb(input float64, ingredient string) float64 {
	return PPMtoPPB(Mgm3toPPM(input, ingredient))
}

func Mgm3toPPM(input float64, ingredient string) float64 {
	switch strings.ToUpper(ingredient) {
	case "SO2":
		return input * 22.4 / so2
	case "O3":
		return input * 22.4 / o3
	case "NO":
		return input * 22.4 / no
	case "NO2":
		return input * 22.4 / no2
	case "CO":
		return input * 22.4 / co
	default:
		return input
	}
}

func PPbtoUgm3(input float64, ingredient string) float64 {
	switch strings.ToUpper(ingredient) {
	case "SO2":
		return input * so2 / 22.4
	case "O3":
		return input * o3 / 22.4
	case "NO":
		return input * no / 22.4
	case "NO2":
		return input * no2 / 22.4
	case "CO":
		return input * co / 22.4
	default:
		return 0
	}
}

func PPMtoUgm3(input float64, ingredient string) float64 {
	return PPbtoUgm3(PPMtoPPB(input), ingredient)
}

func PPbtoMgm3(input float64, ingredient string) float64 {
	return PPMtoMgm3(PPBtoPPM(input), ingredient)
}

func PPMtoMgm3(input float64, ingredient string) float64 {
	switch strings.ToUpper(ingredient) {
	case "SO2":
		return input * so2 / 22.4
	case "O3":
		return input * o3 / 22.4
	case "NO":
		return input * no / 22.4
	case "NO2":
		return input * no2 / 22.4
	case "CO":
		return input * co / 22.4
	default:
		return input
	}
}

//标况(0°)》工况(25°)
func ToWorkCondition(input float64) float64 {
	return input * Twc
}

//工况(25°)》标况(0°)
func ToRealCondition(input float64) float64 {
	return input / Twc
}

//@inputValue 用于转换的初始值
//@ingredient 用于需要转换的项目
//@Unit 转换前的单位
//@ConvertUnit 需要转换成的单位
//单位一共4中 ppb, ppm, ug/m3, mg/m3
func Convert(inputValue float64, ingredient string, Unit string, ConvertUnit string) float64 {
	strings.ReplaceAll(Unit, "μ", "u")
	unit := Unit + "," + ConvertUnit
	switch strings.ToLower(unit) {
	case "ppb,ug/m3":
		return PPbtoUgm3(inputValue, ingredient)
	case "ppb,mg/m3":
		return PPbtoMgm3(inputValue, ingredient)
	case "ppb,ppm":
		return PPBtoPPM(inputValue)
	case "ppm,ug/m3":
		return PPMtoUgm3(inputValue, ingredient)
	case "ppm,mg/m3":
		return PPMtoMgm3(inputValue, ingredient)
	case "ppm,ppb":
		return PPMtoPPB(inputValue)
	case "ug/m3,ppb":
		return Ugm3toPPb(inputValue, ingredient)
	case "ug/m3,ppm":
		return Ugm3toPPM(inputValue, ingredient)
	case "ug/m3,mg/m3":
		return Ugm3toMgm3(inputValue)
	case "mg/m3,ppb":
		return Mgm3toPPb(inputValue, ingredient)
	case "mg/m3,ppm":
		return Mgm3toPPM(inputValue, ingredient)
	case "mg/m3,ug/m3":
		return Mgm3toUgm3(inputValue)
	default:
		return inputValue
	}
}
